package com.jiamao.goplay.ui.discover.vm;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.amap.api.location.AMapLocation;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppApplication;
import com.jiamao.goplay.bean.BaseRep;
import com.jiamao.goplay.bean.FindItem;
import com.jiamao.goplay.bean.res.ResMatch;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.data.BaseSubscriber;
import com.jiamao.goplay.data.DataRepository;
import com.jiamao.goplay.ui.discover.SearchFragment;
import com.jiamao.goplay.util.KeyInputUtils;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.tamsiree.rxkit.view.RxToast;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class MatchVM extends BaseViewModel<DataRepository> {

    private int page = 1;

    //给RecyclerView添加ObservableList
    public ObservableList<MultiItemViewModel> observableList = new ObservableArrayList<>();

    public ObservableField<Integer> index = new ObservableField<>();


    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> age = new ObservableField<>();
    public ObservableField<String> sz = new ObservableField<>();
    public ObservableField<String> dis = new ObservableField<>();
    public ObservableField<String> city = new ObservableField<>();
    public ObservableField<String> des = new ObservableField<>();
    public ObservableField<String> zy = new ObservableField<>();
    public ObservableField<String> hi = new ObservableField<>();

    public ObservableField<String> gototitle = new ObservableField<>();
    public ObservableField<String> goto_nums = new ObservableField<>();
    public ObservableField<String> goto_addrtitle = new ObservableField<>();
    public ObservableField<String> goto_city = new ObservableField<>();
    public ObservableField<String> goto_img = new ObservableField<>();

    public ObservableInt ypdBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt yqBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt jsyqBtnVisibility = new ObservableInt(View.GONE);

    public ObservableInt yyqBtnVisibility = new ObservableInt(View.GONE);

    public ObservableField<String> yqStr = new ObservableField<>("邀请他去");
    public ObservableInt yqnBtnVisibility = new ObservableInt(View.GONE);

    public ObservableField<Integer> isVip = new ObservableField<>();
    public ObservableField<Integer> isShming = new ObservableField<>();
    public ObservableField<Integer> isRenXiang = new ObservableField<>();

    public ResMatch item;

    //RecyclerView多布局添加ItemBinding
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            //通过item的类型, 动态设置Item加载的布局
            String itemType = (String) item.getItemType();

            itemBinding.set(BR.viewModel, R.layout.discover_find_item_viewpage_image_layout);
        }
    });


    public MatchVM(@NonNull Application application, DataRepository repository) {
        super(application, repository);

    }

    public void  update(ResMatch item){
        this.item = item;

        if (item == null) {
            return;
        }

        if (item.getCertification() != null) {
            isVip.set(item.getCertification().getIsVip() == 0 ? View.GONE : View.VISIBLE);
            isRenXiang.set(item.getCertification().getAvatarVerified() == 0 ? View.GONE : View.VISIBLE);
            isShming.set(item.getCertification().getRealNameVerified() == 0 ? View.GONE : View.VISIBLE);
        }

        name.set(item.getBaseInfo().getNickname()+"，"); //昵称
        age.set(item.getUserInfo().getAge() + "");
        sz.set(item.getUserInfo().getGalaxy());
        dis.set(item.getLocation().getDistance() + "km，");
        city.set(item.getLocation().getCity() + "，" + item.getLocation().getArea());
        des.set(item.getUserInfo().getDescribe());
        zy.set(item.getUserInfo().getIndustry());


        if (item.getInviteInfo() != null) {

           gototitle.set(item.getInviteInfo().getPlaceName());
           yqStr.set(item.getInviteInfo().getStatusDesTop());
//            goto_img.set(item.getPlaceWantGo().getPlaceCover());
//            goto_nums.set(item.getPlaceWantGo().getInterestUserNum() + "人想去");
//            goto_city.set(item.getPlaceWantGo().getCity());
//            goto_addrtitle.set(item.getPlaceWantGo().getPlaceName());

            yqStr.set((item.getBaseInfo().getGender()==1?"他":"她")+"邀请你去");


        }


        //模拟10个条目，数据源可以来自网络
        if (item.getAvatarList() != null) {
            uc.pSwitchEvent.setValue(item.getAvatarList().size());
            for (int i = 0; i < item.getAvatarList().size(); i++) {

                FindItemImageViewVM imgItem = new FindItemImageViewVM(this, item.getAvatarList().get(i).getUrl(),null);
                observableList.add(imgItem);

            }
        }





    }
    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Integer> pSwitchEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Integer> uiInitRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Integer> refreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<String> startChat = new SingleLiveEvent<>();
    }



    public void startChat(String id){
        //uc.pSwitchEvent.setValue();
        uc.startChat.setValue(id);

    }

    //条目的点击事件
    public BindingCommand sendClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

          //  sendTextMsg();



           // viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });


    public void sendTextMsg(){

        if (item!=null){

            String text = hi.get();
            KLog.d("aaaaa:"+text);
        //    RxToast.error(hi.get());
            //item.getBaseInfo().getId();
            if (TextUtils.isEmpty(text)){
                RxToast.error("请输入打招呼内容");
                return;
            }

            uc.uiInitRefreshEvent.setValue(1);
          //  KeyInputUtils.hideSoftInputView();
            // 该帐号为示例
            String account = item.getBaseInfo().getId();
// 以单聊类型为例
            SessionTypeEnum sessionType = SessionTypeEnum.P2P;
           // String text = "this is an example";
// 创建一个文本消息
            IMMessage textMessage = MessageBuilder.createTextMessage(account, sessionType, text);
// 发送给对方
            NIMClient.getService(MsgService.class).sendMessage(textMessage, false).setCallback(new RequestCallback<Void>() {
                @Override
                public void onSuccess(Void param) {
                    RxToast.success("发送成功");
                    hi.set("");
                }

                @Override
                public void onFailed(int code) {
                    RxToast.success("发送失败 错误码:"+code);
                }

                @Override
                public void onException(Throwable exception) {
                    RxToast.success("发送异常:");
                }
            });


        }

    }

    //条目的点击事件
    public BindingCommand szClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            RxToast.showToastShort( sz.get());
            //  sz.get()

            //   ToastUtils.showShort("我是头布局");
        }
    });
    //条目的点击事件
    public BindingCommand gotoClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
              finish();
          //  viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
